package com.qiekeji.compiler;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
/*javac在执行编译之前先调用注解处理程序，执行完之后才会生成class文件*/
/*接收的注解*/
@SupportedAnnotationTypes({"com.example.stujava.module.xiangxue.annotation.Lable"})
public class MyProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        Messager messager = processingEnv.getMessager();
        messager.printMessage(Diagnostic.Kind.NOTE, "LUMA" + annotations.size());
        //生成文件、http请求、删除文件...
        try {
            /*生成路径：app\build\generated\ap_generated_sources\debug\...*/
            JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile("processorFile");
            OutputStream os = sourceFile.openOutputStream();
            os.write("package com.i.luma;\n\npublic class B{}".getBytes());
            os.close();
            messager.printMessage(Diagnostic.Kind.ERROR, "success-------close");
        } catch (IOException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, "error-------" + e.getMessage());
        }
        return false;
    }
}